package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.mapper.UserMapper;
import com.yupi.springbootinit.mapper.UserauthinfoMapper;
import com.yupi.springbootinit.model.dto.authuser.SelectAuthUserRequest;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.entity.Userauthinfo;
import com.yupi.springbootinit.service.UserService;
import com.yupi.springbootinit.service.UserauthinfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/authuser")
@Slf4j

public class AuthUserController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserService userService;

    @Resource
    private UserauthinfoService userauthinfoService;
    @Resource
    private UserauthinfoMapper userauthinfoMapper;

    /**
     * 后台用户审核认证
     * (管理员身份)
     */
    @GetMapping("/addAuthUser")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<String> auditAuthUser(@RequestParam("userId") Long id) {
        User user = userService.getById(id);
        user.setIsAuth(1);
        QueryWrapper<Userauthinfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("userId", id);
        queryWrapper2.eq("isAuth",0);
        List<Userauthinfo> userauthinfos = userauthinfoService.list(queryWrapper2);
        Userauthinfo userauthinfo = userauthinfos.get(0);
        if (userauthinfo == null) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"该用户不存在");
        }
        userauthinfo.setIsAuth(1);
        userMapper.updateById(user);
        userauthinfoMapper.updateById(userauthinfo);
        return ResultUtils.success("审核成功");
    }

    @PostMapping("/addAuthInfo")
    public BaseResponse<String> addAuthInfo(@RequestParam(value = "name",required = true) String name,
                                            @RequestParam(value = "stunum",required = true) Long stunum,
                                            @RequestParam(value = "grade",required = true) Integer grade,
                                            @RequestParam(value = "academic",required = true) String academic,
                                            @RequestParam(value = "gender",required = true) String gender, @RequestParam(value = "file",required = true) MultipartFile stuIdCard, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"请先登录");
        }

        Userauthinfo userauthinfo = new Userauthinfo();
        userauthinfo.setName(name);
        userauthinfo.setStunum(stunum);
        userauthinfo.setGrade(grade);
        userauthinfo.setAcademic(academic);
        userauthinfo.setGender(gender);
        userauthinfo.setStuIdCard(userauthinfoService.uploadUserInfoImage(stuIdCard));
        userauthinfo.setUserId(loginUser.getId());
        userauthinfoService.save(userauthinfo);

        return ResultUtils.success("认证信息提交成功");
    }

    @GetMapping("/getAuthInfo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<Userauthinfo>> getAuthInfo() {
        QueryWrapper<Userauthinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isAuth",0);
        List<Userauthinfo> userauthinfo = userauthinfoService.list(queryWrapper) ;
        return ResultUtils.success(userauthinfo);
    }

    @GetMapping("/delAuthInfo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<String> delAuthInfo(@RequestParam("userId") Long id) {
        QueryWrapper<Userauthinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",id);
        List<Userauthinfo> userauthinfos = userauthinfoService.list(queryWrapper);
        Userauthinfo userauthinfo = userauthinfos.get(0);
        if (userauthinfo == null) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"该用户不存在");
        }
        boolean remove = userauthinfoService.remove(queryWrapper);
        boolean b = userauthinfoService.deleteUserInfoImage(userauthinfo.getStuIdCard());
        if (!remove || !b) {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR,"删除失败");
        }
        return ResultUtils.success("删除成功");
    }

    @GetMapping("/findAuthInfo")
    public BaseResponse<Integer> findAuthInfo(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        QueryWrapper<Userauthinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",loginUser.getId());
        Userauthinfo userauthinfo = userauthinfoService.getOne(queryWrapper) ;
        if (userauthinfo == null) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"没有该用户的认证信息");
        }
        Integer isAuth = userauthinfo.getIsAuth();
        return ResultUtils.success(isAuth);
    }

    /**
     * 管理员查询用户认证信息(模糊查询,可附加的查询)
     * @param selectAuthUserRequest
     * @return
     */

    @PostMapping("/AdminselectAuthInfo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<Userauthinfo>> AdminselectAuthInfo(@RequestBody SelectAuthUserRequest selectAuthUserRequest) {

        QueryWrapper<Userauthinfo> queryWrapper = new QueryWrapper<>();
        if (selectAuthUserRequest.getUserId() != null) {
            queryWrapper.like("userId", selectAuthUserRequest.getUserId());
        }
        if (selectAuthUserRequest.getName() != null) {
            queryWrapper.like("name", selectAuthUserRequest.getName());
        }
        if (selectAuthUserRequest.getStunum() != null) {
            queryWrapper.like("stunum", selectAuthUserRequest.getStunum());
        }
        if (selectAuthUserRequest.getGrade() != null) {
            queryWrapper.like("grade", selectAuthUserRequest.getGrade());
        }
        if (selectAuthUserRequest.getAcademic() != null) {
            queryWrapper.like("academic", selectAuthUserRequest.getAcademic());
        }
        if (selectAuthUserRequest.getGender() != null) {
            queryWrapper.like("gender", selectAuthUserRequest.getGender());
        }
        List<Userauthinfo> userauthinfo = userauthinfoService.list(queryWrapper);
        if (userauthinfo == null) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"没有该用户认证信息");
        }
        return ResultUtils.success(userauthinfo);


    }
}
